﻿package morn.core.managers
{
    import flash.display.*;
    import flash.events.*;
    import morn.core.components.*;
    import morn.core.utils.*;

    public class DialogManager extends Sprite
    {
        private var _box:Box;
        private var _mask:Box;
        private var _maskBg:Sprite;

        public function DialogManager()
        {
            this._box = new Box();
            this._mask = new Box();
            this._maskBg = new Sprite();
            addChild(this._box);
            this._mask.addChild(this._maskBg);
            this._maskBg.addChild(ObjectUtils.createBitmap(10, 10, 0, 0.4));
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            return;
        }// end function

        private function onAddedToStage(event:Event) : void
        {
            removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            stage.addEventListener(Event.RESIZE, this.onResize);
            this.onResize(null);
            return;
        }// end function

        private function onResize(event:Event) : void
        {
            var _loc_3:Dialog = null;
            var _loc_4:Bitmap = null;
            var _loc_5:* = stage.stageWidth;
            this._mask.width = stage.stageWidth;
            this._box.width = _loc_5;
            var _loc_5:* = stage.stageHeight;
            this._mask.height = stage.stageHeight;
            this._box.height = _loc_5;
            var _loc_2:* = this._box.numChildren - 1;
            while (_loc_2 > -1)
            {
                
                _loc_3 = this._box.getChildAt(_loc_2) as Dialog;
                if (_loc_3.popupCenter)
                {
                    _loc_3.x = (stage.stageWidth - _loc_3.width) * 0.5;
                    _loc_3.y = (stage.stageHeight - _loc_3.height) * 0.5;
                }
                _loc_2 = _loc_2 - 1;
            }
            _loc_2 = this._mask.numChildren - 1;
            while (_loc_2 > -1)
            {
                
                _loc_3 = this._mask.getChildAt(_loc_2) as Dialog;
                if (_loc_3)
                {
                    if (_loc_3.popupCenter)
                    {
                        _loc_3.x = (stage.stageWidth - _loc_3.width) * 0.5;
                        _loc_3.y = (stage.stageHeight - _loc_3.height) * 0.5;
                    }
                }
                else
                {
                    _loc_4 = this._maskBg.getChildAt(0) as Bitmap;
                    _loc_4.width = stage.stageWidth;
                    _loc_4.height = stage.stageHeight;
                }
                _loc_2 = _loc_2 - 1;
            }
            return;
        }// end function

        public function show(param1:Dialog, param2:Boolean = false) : void
        {
            if (param2)
            {
                this._box.removeAllChild();
            }
            if (param1.popupCenter)
            {
                param1.x = (stage.stageWidth - param1.width) * 0.5;
                param1.y = (stage.stageHeight - param1.height) * 0.5;
            }
            this._box.addChild(param1);
            return;
        }// end function

        public function popup(param1:Dialog, param2:Boolean = false) : void
        {
            if (param2)
            {
                this._mask.removeAllChild(this._maskBg);
            }
            if (param1.popupCenter)
            {
                param1.x = (stage.stageWidth - param1.width) * 0.5;
                param1.y = (stage.stageHeight - param1.height) * 0.5;
            }
            this._mask.addChild(param1);
            this._mask.swapChildrenAt(this._mask.getChildIndex(this._maskBg), this._mask.numChildren - 2);
            addChild(this._mask);
            return;
        }// end function

        public function close(param1:Dialog) : void
        {
            param1.remove();
            if (this._mask.numChildren > 1)
            {
                this._mask.swapChildrenAt(this._mask.getChildIndex(this._maskBg), this._mask.numChildren - 2);
            }
            else
            {
                this._mask.remove();
            }
            return;
        }// end function

        public function closeAll() : void
        {
            this._box.removeAllChild();
            this._mask.removeAllChild(this._maskBg);
            this._mask.remove();
            return;
        }// end function

    }
}
